from bs4 import BeautifulSoup
from pool_v2.scrape_killersports import *
from datetime import date, timedelta
from urllib import URLopener
import csv


import logging
import traceback

import os
import time

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    args = '<outfile>'
    help = 'runs the scripts looking at the '

    def handle(self, *args, **options):
        out = open(args[0], 'w')
        test_scraping_by_time(out)
#        test_scraping()
#        self.stdout.write('Successfully Testing' )

def write(row, outfile):
    '''Appends a line to a spreadsheet, ensuring it is immediately visible (for tail -f).'''
    print row
    writer = csv.writer(outfile)
    writer.writerow(row)
    outfile.flush()

#puts the data into a csv file
def test_scraping_by_time(outfile):
    d=date.today()
    opener = URLopener()
    write(['tm_year','tm_mon','tm_mday','tm_hour','tm_min','tm_sec','num_games'],outfile)
    while True:
        cur_time = time.gmtime()
        num_games = 0
        try:
            soup = BeautifulSoup(opener.open('http://killersports.com/nba.py/query?sid=guest&text=date%%3D%%3D%i%i%02i&submit=query'  % (cur_time.tm_year,cur_time.tm_mon,cur_time.tm_mday)))
            games = create_games_nodb(soup,d)
            print games
            num_games = len(games)
        except Exception, e:
            print Exception, e
        write([cur_time.tm_year,cur_time.tm_mon,cur_time.tm_mday,cur_time.tm_hour,cur_time.tm_min,cur_time.tm_sec,num_games],outfile)
        time.sleep(900)

def test_scraping():
    end=date.today()
    d=date(2012,10,31)
    delta = timedelta(1)
    opener = URLopener()
    logging.basicConfig(level=logging.WARNING)
    log = logging.getLogger('example')
    while(d < end):
        try:
            d=d+delta
            soup = BeautifulSoup(opener.open('http://killersports.com/nba.py/query?sid=guest&text=date%%3D%%3D%i%i%02i&submit=query'  % (d.year,d.month,d.day)))
            create_week_from_html(soup,d)
            print d
        except Exception, e:
            print d
            print Exception, e
            log.exception('Error')


